package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.SysMovie;
import com.gec.model.vo.SysMovieQueryVo;
import com.gec.system.servcie.SysMovieService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gec.system.util.VodTemplate;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "影视管理控制器")
@RequestMapping("admin/system/sysMovie")
public class SysMovieController {

    @Autowired
    private SysMovieService sysMovieService;

    @Autowired
    private VodTemplate vodTemplate;

    //查看
    @ApiOperation("获取全部影视列表")
    @GetMapping("/findAll")
    public Result findAll() {
        List<SysMovie> list = this.sysMovieService.list();
        return Result.ok(list);
    }

    //删除
    @ApiOperation("根据一个Id删除一个影视")
    @DeleteMapping("/removeMovie/{id}")
    public Result removeMovie(@PathVariable Long id) {
        boolean idDel = this.sysMovieService.removeById(id);
        return idDel ? Result.ok() : Result.fail();
    }

    //分页和条件查询
    @ApiOperation("分页和条件查询")
    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findMovieByPageQuery(@PathVariable Long page,
                                       @PathVariable Long limit,
                                       SysMovieQueryVo sysMovieQueryVo) {
        //1.创建分页对象
        IPage<SysMovie> p1 = new Page<SysMovie>(page, limit);
        //2.调用方法
        p1 = this.sysMovieService.selectPage(p1, sysMovieQueryVo);
        //3.返回
        return Result.ok(p1);
    }

    //添加影视
    @ApiOperation("添加影视")
    @PostMapping("/addMovie")
    public Result addMovie(@RequestBody SysMovie sysMovie) {
        System.out.println("sysMovie = " + sysMovie);
        boolean res = this.sysMovieService.save(sysMovie);
        return res ? Result.ok() : Result.fail();
    }

    //修改
    @ApiOperation("修改影视")
    @GetMapping("/findMovieById/{id}")
    public Result findMovieById(@PathVariable Long id) {
        SysMovie sysMovie = this.sysMovieService.getById(id);
        return Result.ok(sysMovie);
    }

    @PostMapping("/updateMovie")
    public Result updateRole(@RequestBody SysMovie sysMovie) {
        boolean updateById = this.sysMovieService.updateById(sysMovie);
        return updateById ? Result.ok() : Result.fail();
    }

    //批量删除
    @DeleteMapping("/removeMovieByIds")
    public Result removeMovieByIds(@RequestBody List<Long> ids) {
        boolean b = this.sysMovieService.removeByIds(ids);
        return b ? Result.ok() : Result.fail();
    }

    //播放视频
    @RequestMapping(value = "getPlayAuth/{id}")
    public Result playVideoByAuth(@PathVariable Long id) throws Exception {
        SysMovie sysMovie = this.sysMovieService.getById(id);
        String image = sysMovie.getImage();
        String playId = sysMovie.getPlayId();
        System.out.println("playId = " + playId);

        String playAuth = this.vodTemplate.getVideoPlayAuth(playId).getPlayAuth();

        // 封装map 集合
        HashMap<String, Object> map = new HashMap<>();
        // 分别封装三个参数  参数的key 要和前端对应
        map.put("image", image);
        map.put("playId", playId);
        map.put("playAuth", playAuth);
        return Result.ok(map);
    }


//    @ApiOperation("寻找")
//    @GetMapping("getAllPlayAuth")
//    public Result getAllPlayAuth(@RequestParam String name){
//        List<SysMovie> list =this.sysMovieService.getMovieByName(name);
//        return Result.ok(list);
//    }


}
